package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.QuestionComment;
import cn.wolfcode.luowowo.comment.repository.QuestionCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.IQuestionCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Optional;

@Service
public class QuestionCommentServiceImpl implements IQuestionCommentService {

    @Autowired
    private QuestionCommentMongoRepository repository;

    public List<QuestionComment> list() {
        List<QuestionComment> list = repository.findAll();
        return list;
    }

    public AjaxResult saveOrUpdate(QuestionComment question) {
        AjaxResult result = new AjaxResult();
        try {
            repository.save(question);
            result.setData(question);
        } catch (Exception e) {
             e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public QuestionComment findById(String id) {
        QuestionComment questionComment=null;
        Optional<QuestionComment> op = repository.findById(id);
        if( op.isPresent()) {
            questionComment= op.get();
        }
        return  questionComment;

    }

    public List<QuestionComment> findAll() {

        return repository.findAll();
    }

    public void updateStats(QuestionComment question) {
        repository.save(question);
    }
}
